package com.xuxin.pda.activity;

import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.xuxin.pda.R;
import com.xuxin.pda.adapter.MainAdapter;
import com.xuxin.pda.base.BaseConstant;
import com.xuxin.pda.base.BasePDAActivity;
import com.xuxin.pda.bean.MainBean;
import com.xuxin.pda.utils.ACache;
import com.xuxin.pda.utils.RecycleUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by xu_xin
 * Created on 2020/12/24 0020
 * 主页
 */
public class MainActivity extends BasePDAActivity {
    @BindView(R.id.main_recycle)
    RecyclerView main_recycle;

    private MainAdapter mainAdapter;
    private String userNo;//用户编号
    private String userName;//用户名

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_main);
    }

    @Override
    public void initView() {
        mainAdapter = new MainAdapter();
        RecycleUtils.init(main_recycle, 2);
        main_recycle.setAdapter(mainAdapter);
        mainAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                switch (position) {
                    case 0:
                        ActivityUtils.startActivity(TrashDayActivity.class);
                        break;
                    case 1:
                        ActivityUtils.startActivity(TrashMonthActivity.class);
                        break;
                    case 2:
                        ActivityUtils.startActivity(TrashCollectActivity.class);
                        break;
                    case 3:
                        ActivityUtils.startActivity(TrashLinkActivity.class);
                        break;
                    case 4:
                        ActivityUtils.startActivity(TrashEnterActivity.class);
                        break;
                    case 5:
                        ActivityUtils.startActivity(TrashExitActivity.class);
                        break;
                    case 6:
                        ActivityUtils.startActivity(PrintActivity.class);
                        break;
                }
            }
        });
    }

    @Override
    public void initData() {
        ACache aCache = ACache.get(context);
        userNo = aCache.getAsString(BaseConstant.userNo);
        userName = aCache.getAsString(BaseConstant.userName);
        List<MainBean> mainBeans = isAdmin("user1001".equals(userNo) || userName.contains("管理员"));
        mainAdapter.setList(mainBeans);
    }

    @Override
    public void onComplete() {
    }

    @Override
    public void onClick(View view) {
    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                ToastUtils.showShort("再按一次退出");
                exitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onPDASuccess(String message) {
        if ("user1001".equals(message)) {
            ActivityUtils.startActivity(PrintActivity.class);
        }
    }

    public List<MainBean> isAdmin(Boolean isAdmin) {
        List<MainBean> mainBeanList = new ArrayList<>();
        mainBeanList.add(new MainBean("医废日报表", R.mipmap.img_main_day));
        mainBeanList.add(new MainBean("医废月报表", R.mipmap.img_main_month));
        mainBeanList.add(new MainBean("医废收集", R.mipmap.img_main_collect));
        mainBeanList.add(new MainBean("箱袋关联", R.mipmap.img_main_link));
        mainBeanList.add(new MainBean("医废入库", R.mipmap.img_main_enter));
        mainBeanList.add(new MainBean("医废出库", R.mipmap.img_main_exit));
        if (isAdmin) {
            mainBeanList.add(new MainBean("预设打印", R.mipmap.img_main_print));
        }
        return mainBeanList;
    }
}